#include	<stdio.h>
#include	<string.h>
#include	<stdlib.h>
#include	<stdbool.h>
#include	<unistd.h>
#include	<assert.h>

#define PHASE_1_A 1
#define PHASE_1_B 2
#define PHASE_2_A 3
#define PHASE_2_B 4

//At the final version there are 4 types of messages circulating along the ring:
// 1 - proposer_msg : 'p'
// 2- short_notification: 's'
// 3- inst : 'i'
// 4- inst + paxos : 'n'
// 5- paxos_short_not: 'm'

//8152, 8492
//------------------------------------------------------------------------------
#define	PROPOSER_MSG_HEADER	(6 * sizeof(int) +  sizeof(char)  + sizeof(double))

typedef struct proposer_msg_t {
	char type;
	int highest_deliverable_inst_id;
	int location_index_in_proposer; //to ease marking in the proposer for the learner section
	int num_nodes_visited;
	int proposer_id;
	int msg_id;
	int final_decision;
	double mili_seconds;
	char fake[32000]; //previously 8492 16384
} proposer_msg;

#define MAX_MSG_COUNT 1

typedef struct pack_t {
	int pack_id; //The ID assigned in the Coordinator. This is usually equal to instance_id.
	proposer_msg p_msgs[MAX_MSG_COUNT];
} pack;

typedef struct inst_t {

	char type;
	pack packet;
	bool deliver;
	bool received;

	int instance_id;
	int coordinator_id;
	int dec;
	int round_number;
	int highestDeliverable;

} inst;

typedef struct paxos_msg_t {
	int instance_number;
	int round_number;
	int acceptor_count;
	int type;

	int max_vrnd;
	int max_vval;
	int VALUE;

} paxos_msg;

typedef struct inst_paxos_t {
	char type;
	inst inst_packet;
	paxos_msg paxos_2b;
} inst_paxos;

typedef struct paxos_data_t {
	int rnd;
	int vrnd;
	int vval;

	int crnd;
	int cval;

} paxos_data;

typedef struct gap_t {
	int start;
	int end;
} gap;

typedef struct connection {
	int new_sid;
	struct sockaddr_in client_addr;
} connection;

typedef struct short_dec_notification_t { // this is only used if a decision about a message is being propagated to its proposer
	char type;
	int instance_id;
	int pack_id;
	int proposer_msg_id;

	int highestDeliverable;
	int location_index_in_proposer;
	int proposer_id;
} short_dec_notification;

typedef struct paxos_short_not_t {
	char type;
	paxos_msg paxos_2b;
	short_dec_notification short_decision;

} paxos_short_not;

